package com.cskaoyan.realm;

import com.cskaoyan.bean.User;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author
 * @date 2020/12/7 16:28
 */
@Component
public class WxRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken token = (MallToken) authenticationToken;
        String username = token.getUsername();
        List<User> users = userService.selectUserByUsername(username);
        String credential = users.size() >= 1 ? users.get(0).getPassword() : null;
        Integer userId = users.size() >= 1 ? users.get(0).getId() : null;
        if (userId == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo(userId,credential,this.getName());

        return authenticationInfo;
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

}
